package com.name.givename.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.name.givename.entity.UserInfoEntity;
import com.name.givename.entity.UserInformationEntity;
import com.name.givename.enums.ResultEnum;
import com.name.givename.exception.ServiceException;
import com.name.givename.param.BindPhoneParams;
import com.name.givename.param.PhoneVerifyCodeParam;
import com.name.givename.param.UserInfoParams;
import com.name.givename.result.ResultVoUtils;
import com.name.givename.service.UserInfoService;
import com.name.givename.utils.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by yangtianrui on 18/9/2.
 */
@RestController
@RequestMapping("/userInfo")
@Api("用户中心")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService; // 用户信息服务

    @ApiOperation("获取用户信息")
    @PostMapping("/getUserInfo")
    public Result<UserInfoEntity> getUserInfo(@RequestBody UserInfoParams userInfoParams) {

        if(userInfoParams == null){
            throw new ServiceException(ResultEnum.USER_ID_NULL.getCode(),ResultEnum.USER_ID_NULL.getValue());
        }
        return ResultVoUtils.success(userInfoService.getUserInfo(userInfoParams.getUserId()));

    }

    @ApiOperation("通过OpenId获取用户信息")
    @PostMapping("/getUserInformation")
    public Result<UserInformationEntity> getUserInformation(@RequestBody UserInfoParams userInfoParams){
        if (userInfoParams.getOpenId() == null){
            throw new ServiceException(ResultEnum.USER_OPENID_NULL.getCode(),ResultEnum.USER_OPENID_NULL.getValue());
        }
        return ResultVoUtils.success(userInfoService.getUserInformation(userInfoParams.getOpenId()));
    }

    @ApiOperation("绑定手机号接口")
    @PostMapping("/bindPhone")
    public Result<Integer> bindPhone(@RequestBody BindPhoneParams bindPhoneParams){
        if (bindPhoneParams.getOpenId() == null && bindPhoneParams.getPhone() == null && bindPhoneParams.getSmsCode() == null){
            throw new ServiceException(ResultEnum.USER_OPENID_NULL.getCode(),ResultEnum.USER_OPENID_NULL.getValue());
        }
        return ResultVoUtils.success(userInfoService.bindPhone(bindPhoneParams));
    }
    
    @ApiOperation("发送短信验证码接口")
    @PostMapping("/sendVerifyCode")
    public Result<Integer> sendCode(@RequestBody PhoneVerifyCodeParam phoneVerifyCodeParam){
    	if (phoneVerifyCodeParam.getPhone() ==null || "".equals(phoneVerifyCodeParam.getPhone())){
            throw new ServiceException(ResultEnum.PHONE_NULL.getCode(),ResultEnum.PHONE_NULL.getValue());
        }
    	SendSmsResponse response = userInfoService.sendVerifyCode(phoneVerifyCodeParam);
    	if("OK".equals(response.getCode())) {
    		return ResultVoUtils.success("短信验证码发送成功！");
    	}else {
    		return ResultVoUtils.error(1, response.getMessage());
    	}
    }
}
